import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface DashboardStats {
  totalOpportunities: number;
  activeOpportunities: number;
  weeklyNew: number;
  monthlyClosedWon: number;
  totalValue: number;
  conversionRate: number;
}

export interface TodoItem {
  id: string;
  type: 'follow_up' | 'approval' | 'deadline';
  title: string;
  description: string;
  dueDate: string;
  priority: 'high' | 'medium' | 'low';
  opportunityId?: string;
}

export interface ActivityItem {
  id: string;
  type: 'opportunity_created' | 'opportunity_updated' | 'plan_generated' | 'risk_reported';
  title: string;
  description: string;
  timestamp: string;
  user: string;
}

export interface DashboardState {
  stats: DashboardStats;
  todos: TodoItem[];
  activities: ActivityItem[];
  opportunityDistribution: Array<{ name: string; value: number; color: string }>;
  highValueOpportunities: Array<{
    id: string;
    name: string;
    value: number;
    customer: string;
    risk: 'high' | 'medium' | 'low';
  }>;
  loading: boolean;
  error: string | null;
}

const initialState: DashboardState = {
  stats: {
    totalOpportunities: 0,
    activeOpportunities: 0,
    weeklyNew: 0,
    monthlyClosedWon: 0,
    totalValue: 0,
    conversionRate: 0,
  },
  todos: [],
  activities: [],
  opportunityDistribution: [],
  highValueOpportunities: [],
  loading: false,
  error: null,
};

const dashboardSlice = createSlice({
  name: 'dashboard',
  initialState,
  reducers: {
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
    setStats: (state, action: PayloadAction<DashboardStats>) => {
      state.stats = action.payload;
    },
    setTodos: (state, action: PayloadAction<TodoItem[]>) => {
      state.todos = action.payload;
    },
    addTodo: (state, action: PayloadAction<TodoItem>) => {
      state.todos.unshift(action.payload);
    },
    removeTodo: (state, action: PayloadAction<string>) => {
      state.todos = state.todos.filter(todo => todo.id !== action.payload);
    },
    setActivities: (state, action: PayloadAction<ActivityItem[]>) => {
      state.activities = action.payload;
    },
    addActivity: (state, action: PayloadAction<ActivityItem>) => {
      state.activities.unshift(action.payload);
      // 保持最新的20条活动记录
      if (state.activities.length > 20) {
        state.activities = state.activities.slice(0, 20);
      }
    },
    setOpportunityDistribution: (state, action: PayloadAction<Array<{ name: string; value: number; color: string }>>) => {
      state.opportunityDistribution = action.payload;
    },
    setHighValueOpportunities: (state, action: PayloadAction<DashboardState['highValueOpportunities']>) => {
      state.highValueOpportunities = action.payload;
    },
  },
});

export const {
  setLoading,
  setError,
  setStats,
  setTodos,
  addTodo,
  removeTodo,
  setActivities,
  addActivity,
  setOpportunityDistribution,
  setHighValueOpportunities,
} = dashboardSlice.actions;

export default dashboardSlice.reducer;
